{% extends "base.html" %}
{% load i18n %}
{% block title %} Tu Negocio Empresarios - Devolver Prestamos a Empresarios{% endblock %}

{% block head-css-ext %}
  <link rel="stylesheet" type="text/css" href="{{MEDIA_URL}}css/layout.css" media="screen" />
  <link rel="stylesheet" type="text/css" href="{{MEDIA_URL}}css/jquery-ui.css" media="screen" />
  <link rel="stylesheet" type="text/css" href="{{MEDIA_URL}}css/blue/style.css" media="print, projection, screen" />
{% endblock %}

{% block head-js-ext %}
  <script type="text/javascript" src="{{MEDIA_URL}}js/jquery.js"></script>
  <script type="text/javascript" src="{{MEDIA_URL}}js/jquery-ui.js"></script>
  <script type="text/javascript" src="{{MEDIA_URL}}js/jquery.tablesorter.js"></script>
  <script type="text/javascript" src="{{MEDIA_URL}}js/jquery.tablePagination.js"></script>
  <script type="text/javascript" src="{{MEDIA_URL}}js/jquery.translate.js"></script>  
  <script type="text/javascript" src="{{MEDIA_URL}}js/jquery.bgiframe.js"></script>
  <script type="text/javascript" src="{{MEDIA_URL}}js/date.format.js"></script> 
{% endblock %}

{% block head-js-embedded %}
  <script type="text/javascript">
    $(document).ready(function(){
	  var options = {
		firstArrow : (new Image()).src="{{MEDIA_URL}}img/icons/first.png",
		prevArrow : (new Image()).src="{{MEDIA_URL}}img/icons/prev.png",
		lastArrow : (new Image()).src="{{MEDIA_URL}}img/icons/last.png",
		nextArrow : (new Image()).src="{{MEDIA_URL}}img/icons/next.png"
	  }	
	  $("button, input:submit", ".enviar").button();
	  
	  $("#prestamos")
		.tablesorter({widthFixed: true,  widgets: ['zebra'], headers: { 5 : {sorter: false} } })
        .tablePagination(options);
		
	  $("#tablePagination").translate('en', 'es');
	  
      $("select#tipoprestamo").change(function(){
		var valor = $(this).val();
		if (valor != 0)
		{
		  $.getJSON("/ajax/buscarprestamo/"+ $(this).val()+ "/", function(data){
			
			var options = '';
			for (var i = 0; i < data.length; i++)
			{
			  var fecha = new Date(data[i].fields['fecha']);
			  options += '<tr align="center">' +
							'<td>' + data[i].fields['empresario'] + '</td>' +
							'<td>' + data[i].fields['producto'] + '</td>' +
							'<td>' + data[i].fields['cantidad'] + '</td>' +
							'<td>' + fecha.format("dd mmm yyyy") + '</td>' +
							'<td>' + data[i].fields['tipo_prestamo'] + '</td>' +
							'<td>' + '<input type="checkbox" name="devolver_prestamo" value="' + parseInt(data[i].pk) + '" id="event">' + '</td>' +
						  '</tr>';			
			}
			$("#prestamos tbody").html(options).show();			  
		  });			
		}
		else
		{
		  $("#prestamos tbody").hide();
		}
      });
	  
	  $("form").submit(function(){
		if (!isCheckedById("event"))
		{
		  alert ("Seleccione al menos uno para devolución");
		  return false;
		}
		else
		{
		  return true; //submit the form
		}
	  });
	  
	  function isCheckedById(id)
	  {
		var checked = $("input[id="+id+"]:checked").length;
		if (checked == 0)
		{
		  return false;
		}
		else
		{
		  return true;
		}
	  }	
    });     
  </script>
{% endblock %}

{% block content-box1 %}
  {% if msg_list %}
	{% for message in msg_list %}
		{{ message }}
	{% endfor %}
  {% endif %}
  <br><br>
  <select id="tipoprestamo" name="tipoprestamo">
	<option value="0" selected="selected">-----------------------</option>
    <option value="3">Prestamo de Empresario</option>
    <option value="4">Prestamo a Empresario</option>
  </select>
  <br><br>
  <form method="POST" id="testform">{% csrf_token %}
	<table id="prestamos" class="tablesorter">
	  <thead>
		<tr align="center">
		  <th>Empresario&nbsp;&nbsp;&nbsp;</th>
		  <th>Producto&nbsp;&nbsp;&nbsp;</th>
		  <th>Cantidad&nbsp;&nbsp;&nbsp;&nbsp;</th>
		  <th>Fecha&nbsp;&nbsp;&nbsp;&nbsp;</th>
		  <th>Tipo de Prestamo</th>
		  <th>Marcar para devolución</th>
        </tr>            
      </thead>   
      <tbody>
      </tbody>
    </table>
	<br>
	<div align="center" class="enviar">
	  <input type="submit" name="devolverprestamo" id="devolver_prestamo" value="Aplicar Devolucion"></p>
	</div>
  </form>  
{% endblock %}

{% block content-colum2 %}
  {% if user.is_authenticated %}
	<ul>
	  <li><a href="{% url index %}">{% trans "Home" %}</a></li>
	  <li><a href="{% url empresarios %}">Empresarios</a></li>
	  <li><a href="{% url agregar_prestamo_empresario %}">Agregar Prestamo Empresarios</a></li>
    </ul>
  {% endif %}   
{% endblock %}
